package com.yaozheng.vocationaltraining.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.RelativeLayout;

/**
 * 上一次学习进度
 */
public class LastLearnProgressView extends RelativeLayout {
    /**
     * 内圆进度画笔对象
     */
    private Paint insideProgressPaint;
    /**
     * 内圆没进度画笔对象
     */
    private Paint insidePaint;
    int max = 100;
    /**
     * 边框大小
     */
    float progressBorder;


    /**
     * 当前进度
     */
    private float currProgress;

    public LastLearnProgressView(Context context) {
        super(context);
        initView();
    }

    public LastLearnProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public LastLearnProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    /**
     * 初始化View
     */
    public void initView() {
        progressBorder = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                4, getResources().getDisplayMetrics());
        insideProgressPaint = new Paint();
        insideProgressPaint.setColor(Color.parseColor("#c51949"));

//                getResources().getColor(
//                R.color.progress_select_border_color));
        insideProgressPaint.setAntiAlias(true);// 设置抗锯齿;;;;
        insideProgressPaint.setStyle(Paint.Style.STROKE);// 设置空心.
        insideProgressPaint.setStrokeWidth(progressBorder);

        insidePaint = new Paint();
        insidePaint.setColor(Color.parseColor("#b7b7b7"));
//                getResources().getColor(
//                R.color.progress_not_select_border_color));
        insidePaint.setAntiAlias(true);// 设置抗锯齿;;;;
        insidePaint.setStyle(Paint.Style.STROKE);// 设置空心.
        insidePaint.setStrokeWidth(progressBorder);
        currProgress = 60;
        setWillNotDraw(false);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        int width = getWidth();
        float progressRoundWdith = width - progressBorder * 2;
        RectF progressRoundOval = new RectF((width - progressRoundWdith - progressBorder) / 2, (width - progressRoundWdith - progressBorder) / 2, (width + progressRoundWdith + progressBorder) / 2, (width + progressRoundWdith + progressBorder) / 2);
        int benginstartAngle = 75;
        /**
         * 红色进度弧形
         */
        float angle1 = 360 * benginstartAngle / max;
        float angle2 = 360 * currProgress / max;
        canvas.drawArc(progressRoundOval, angle1, angle2, false, insideProgressPaint);
        if (currProgress != 100) {
            /**
             * 灰色进度弧形
             */
//            canvas.drawArc(progressRoundOval, 360 * (currProgress + benginstartAngle+1) / max, 360
//                    * (max - currProgress) / max, false, insidePaint);
            canvas.drawArc(progressRoundOval, angle1 + angle2, 360 - angle2, false, insidePaint);
        }

        super.onDraw(canvas);
    }

    public void setCurrProgress(float currProgress) {
        this.currProgress = currProgress;
        invalidate();
    }
}
